package com.tianyu.inspection.service;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianyu.inspection.common.component.AppConstants;
import com.tianyu.inspection.common.model.PageModel;
import com.tianyu.inspection.entity.ConstructionType;
import com.tianyu.inspection.entity.Project;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.mapper.ConstructionTypeMapper;
import com.tianyu.inspection.mapper.ProjectMapper;
import com.tianyu.inspection.service.api.IConstructionTypeService;
import io.ocs.model.OcsSimpleModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 基础设施类型服务
 * @Date 2021/1/18 - 09:32
 **********************************/

@Service
public class ConstructionTypeServiceImpl implements IConstructionTypeService {

    @Resource
    private ConstructionTypeMapper constructionTypeMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private AppConstants appConstants;

    @Resource
    private OcsSimpleModel ocsSimpleModel;

    /**
     * 分页查询
     *
     * @param queryMo 参数模型
     * @return 分页记录对象
     */
    @Override
    public IPage<ConstructionType> searchListPage(QueryMo queryMo) {
        final IPage<ConstructionType> modelPage = PageModel.getModelPage(queryMo, constructionTypeMapper);
        modelPage.getRecords().forEach(constructionType -> {
            if (Objects.nonNull(constructionType.getIcon())){
                constructionType.setIcon(ocsSimpleModel.getObjectUrl(constructionType.getIcon()));
            }
        });
        return modelPage;
    }

    /**
     * 根据项目查询施工作业区类型
     *
     * @param queryMo   查询参数集
     * @param projectId 项目ID
     * @return
     */
    @Override
    public List<ConstructionType> searchList(QueryMo queryMo, Integer projectId) {
        queryMo.setPageSize(-1);
        Project project = projectMapper.selectById(projectId);
        List<ConstructionType> returnTypeList = new ArrayList<>();
        if (Objects.nonNull(project) && Objects.nonNull(project.getConstructionTypeUse())){
            JSONArray jsonArray = JSONArray.parseArray(project.getConstructionTypeUse());
            List<ConstructionType> typeList = PageModel.getModelPage(queryMo, constructionTypeMapper).getRecords();
            typeList.forEach(constructionType -> {
                if (jsonArray.contains(constructionType.getId())){
                    if (Objects.nonNull(constructionType.getIcon())){
                        constructionType.setIcon(ocsSimpleModel.getObjectUrl(constructionType.getIcon()));
                    }
                    returnTypeList.add(constructionType);
                }
            });
        }
        return returnTypeList;
    }

    /**
     * 通过ID等参数查询一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 单条记录结果
     */
    @Override
    public ConstructionType selectOne(QueryMo queryMo) {
        if (Objects.nonNull(queryMo.getEqMap().get("id"))){
            return constructionTypeMapper.selectOne(new QueryWrapper<ConstructionType>().allEq(queryMo.getEqMap()));
        }
        return null;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 实体对象
     * @return 是否操作成功
     */
    @Override
    public boolean insert(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        ConstructionType constructionType = objectMapper.convertValue(operateMo.getEntity(), ConstructionType.class);
        return 1 == constructionTypeMapper.insert(constructionType);
    }

    /**
     * 通过ID删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean delete(QueryMo queryMo) {
        if(Objects.nonNull(queryMo.getEqMap().get("id"))){
            return 1 == constructionTypeMapper.delete(new QueryWrapper<ConstructionType>().allEq(queryMo.getEqMap()));
        }
        return false;
    }

    /**
     * 通过ID更新一条数据
     *
     * @param operateMo 实体对象参数集
     * @return 是否操作成功
     */
    @Override
    public boolean update(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        ConstructionType constructionType = objectMapper.convertValue(operateMo.getEntity(), ConstructionType.class);
        operateMo.getEqMap().remove("project_id");
        return 1 == constructionTypeMapper.update(constructionType,new QueryWrapper<ConstructionType>().allEq(operateMo.getEqMap()));
    }

    /**
     * 通过ID逻辑删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean logicallyDelete(QueryMo queryMo) {
        return 1 == constructionTypeMapper.update(
                new ConstructionType().setState(1),
                new QueryWrapper<ConstructionType>().allEq(queryMo.getEqMap())
        );
    }
}
